package com.hcbd.tdwt.ui.setting.xingyu.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


import com.hcbd.tdwt.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.pancoit.compression.CompressionInterface;
import com.pancoit.compression.ZDCompression;


import com.hcbd.tdwt.base.AuthManger;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.ui.xingyu.dialog.AuthInfoDialog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_auth_info)
public class AuthInfoActivity extends BaseActivity {
    @ViewById
    ImageView picInfoImg,voiceInfoImg;
    @ViewById
    LinearLayout picInfoLL,voiceInfoLL;
    @ViewById
    TextView voEffTimeTV,voExpireDateTV,voTotalCallTV,voNumberUsedTV,voRemainUsageTV,voValPeriod;
    @ViewById
    TextView picEffTimeTV,picExpireDateTV,picTotalCallTV,picNumberUsed,picRemainUsage,picValPeriod;
    @ViewById
    Button authVoiceBtn,authPicBtn;
    @ViewById
    TextView voTakeEffectTV,picTakeEffectTV;
    @ViewById
    RelativeLayout voiceInfoAreaRL,picInfoAreaRL;
    @ViewById
    ImageView headLeft;
    private AuthInfoDialog authPicInfoDialog;
    private AuthInfoDialog authVoiceInfoDialog;
    @AfterViews
    protected void init() {
        initDialog();
        initData();
    }



    @Click
    void authVoiceBtn(){
        AuthInfoDialog.show(authVoiceInfoDialog);
    }
    @Click
    void authPicBtn(){
        AuthInfoDialog.show(authPicInfoDialog);
    }

    public void initData(){
        if(AuthManger.getAuthManger().isVoiceOnline()){
            intOnlineInfo(false);
        }else{
            String str=ZDCompression.getInstance().getVOffInfo();
            intOffInfo(str,false);
        }
        if(AuthManger.getAuthManger().isPicOnline()){
            intOnlineInfo(true);
        }else{
            String str=ZDCompression.getInstance().getImgOffInfo();
            intOffInfo(str,true);
        }
        // 左上角返回键
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @Click
    void voiceInfoAreaRL(){
        if(voiceInfoImg.isSelected()){
            voiceInfoImg.setSelected(false);
            voiceInfoImg.setBackground(ContextCompat.getDrawable(this,R.drawable.right_while_arrow));
            voiceInfoLL.setVisibility(View.GONE);
        }else{
            voiceInfoLL.setVisibility(View.VISIBLE);
            voiceInfoImg.setSelected(true);
            voiceInfoImg.setBackground(ContextCompat.getDrawable(this,R.drawable.down_arrow));
        }

    }
    @Click
    void picInfoAreaRL(){
        if(picInfoImg.isSelected()){
            picInfoImg.setSelected(false);
            picInfoImg.setBackground(ContextCompat.getDrawable(this, R.drawable.right_while_arrow));
            picInfoLL.setVisibility(View.GONE);
        }else{
            picInfoImg.setSelected(true);
            picInfoLL.setVisibility(View.VISIBLE);
            picInfoImg.setBackground(ContextCompat.getDrawable(this, R.drawable.down_arrow));

        }
    }

    void initDialog(){
        authPicInfoDialog =new AuthInfoDialog(this, "图像授权", "图像压缩库激活,请输入授权码。无授权码请联系销售人员", new AuthInfoDialog.OnItemClickListener() {
            @Override
            public void onOK(String str,AuthInfoDialog authInfoDialog) {
                showLoading("加载中");
                ZDCompression.getInstance().initImgZip(AuthInfoActivity.this,str, new CompressionInterface() {
                    @Override
                    public void initCallback(int code) {
                        runOnUiThread(() -> {
                            hideLoading();
                            if(code!=20000){
                                if(20001==code||20100==code){
                                    toast("key错误！:" + code);
                                }else if(20010==code){
                                    toast("没有网络！:" + code);
                                }else{
                                    toast("其他原因:" + code);
                                }
                            }else{
                                toast("图片压缩库授权成功");
                                AuthManger.getAuthManger().initPicOnline(str);
                                initData();
                            }
                        });

                    }
                    @Override
                    public void zipCallback(int code) {
                        Log.e("zipCallback",":"+code);
                        if(code!=20000){
                            toast("图片压缩库压缩失败:"+code);
                        }
                    };
                });
                AuthInfoDialog.shutDown(authInfoDialog);
            }

            @Override
            public void onScanning() {
                AuthManger.getAuthManger().initScan(AuthInfoActivity.this,AuthManger.PIC_SC_CODE);
            };
        });
        authVoiceInfoDialog =new AuthInfoDialog(this, "语音授权", "语音压缩库激活,请输入授权码。无授权码请联系销售人员", new AuthInfoDialog.OnItemClickListener() {
            @Override
            public void onOK(String str,AuthInfoDialog authInfoDialog) {
                showLoading("加载中");
                ZDCompression.getInstance().initVoiceZip(AuthInfoActivity.this,str, new CompressionInterface() {
                    @Override
                    public void initCallback(int code) {
                        runOnUiThread(() -> {
                            hideLoading();
                            if (code != 20000) {
                                if(20001==code||20100==code){
                                    toast("key错误！:" + code);
                                }else if(20010==code){
                                    toast("没有网络！:" + code);
                                }else{
                                    toast("其他原因:" + code);
                                }

                            }else{
                                toast("语音压缩库授权成功");
                                AuthManger.getAuthManger().initVoOnline(str);
                                initData();
                            }
                        });
                    }
                    @Override
                    public void zipCallback(int code) {
                        if (code != 20000) {
                            runOnUiThread(() -> toast("语音压缩库压缩失败！:" + code));

                        }
                    }
                });
                AuthInfoDialog.shutDown(authInfoDialog);
            };
            @Override
            public void onScanning() {
                AuthManger.getAuthManger().initScan(AuthInfoActivity.this,AuthManger.VO_SC_CODE);
            };
        });
    }
    public void  intOnlineInfo(boolean isPic) {
        if(isPic){
            picEffTimeTV.setText("--");
            picExpireDateTV.setText("--");
            picValPeriod.setText("--");
            picTakeEffectTV.setText("*授权终身有效");
            picTakeEffectTV.setTextColor(getColor(R.color.white));
            picValPeriod.setText("终身有效");
            picTotalCallTV.setText("--");
            picNumberUsed.setText("--");
            picRemainUsage.setText("--");
            authPicBtn.setVisibility(View.GONE);
        }else{
            voEffTimeTV.setText("--");
            voTakeEffectTV.setText("*授权终身有效");
            voTakeEffectTV.setTextColor(getColor(R.color.white));
            voValPeriod.setText("终身有效");
            voExpireDateTV.setText("--");
            voTotalCallTV.setText("--");
            voNumberUsedTV.setText("--");
            voRemainUsageTV.setText("--");
            authVoiceBtn.setVisibility(View.GONE);
        }
    }
    public void  intOffInfo(String str,boolean isPic) {
        if ("".equals(str) || str == null) {
            return;
        }
        String[] array = str.split(",");
        int usageCount=Integer.parseInt(array[0]);
        int numberUsed = 100 - usageCount;
        String startDate=array[1];
        String endDate=array[2];
        String currentDate= DateUtils.getDateLongSerial();
        int code= DateUtils.dataContrast(currentDate, endDate);
        if(isPic){
            picEffTimeTV.setText(startDate);
            picExpireDateTV.setText(endDate);
            picValPeriod.setText(endDate);
            picTotalCallTV.setText("100");
            picNumberUsed.setText(String.valueOf(numberUsed));
            picRemainUsage.setText(String.valueOf(usageCount));
            if (code==-1||code==0||0>=usageCount) {
                picTakeEffectTV.setText("*授权已超过有效期");
                picTakeEffectTV.setTextColor(getColor(R.color.colorPrimary));
            }else{
                picTakeEffectTV.setText("*授权已生效");
                picTakeEffectTV.setTextColor(getColor(R.color.white));
            }
        }else {
            voEffTimeTV.setText(startDate);
            voExpireDateTV.setText(endDate);
            voValPeriod.setText(endDate);
            voTotalCallTV.setText("100");
            voNumberUsedTV.setText(String.valueOf(numberUsed));
            voRemainUsageTV.setText(String.valueOf(usageCount));
            if (code==-1||code==0||0>=usageCount) {
                voTakeEffectTV.setText("*授权已超过有效期");
                voTakeEffectTV.setTextColor(getColor(R.color.colorPrimary));
            }else{
                voTakeEffectTV.setText("*授权已生效");
                voTakeEffectTV.setTextColor(getColor(R.color.white));
            }

        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if(requestCode ==AuthManger.PIC_SC_CODE||requestCode ==AuthManger.VO_SC_CODE){
                IntentResult scanResult = IntentIntegrator.parseActivityResult(resultCode, data);
                if (scanResult.getContents() != null) {
                    String result = scanResult.getContents();
                    if(requestCode ==AuthManger.PIC_SC_CODE){
                        authPicInfoDialog.setKeyEt(result);
                    }else{
                        authVoiceInfoDialog.setKeyEt(result);
                    }
                }
            }
        }
    }


}
